package com.atguigu.jxc.service.impl;


import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.CustomerService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private LogService logService;
    @Autowired
    private CustomerDao customerDao;

    @Override
    public ServiceVO save(Customer customer) {
        // supplier为空时，说明是新增操作，需要先判断角色名是否存在
        if (customer.getCustomerId() == null) {

            Customer excustomer = customerDao.findCustomerByName(customer.getCustomerName());

            if (excustomer != null) {

                return new ServiceVO<>(ErrorCode.ROLE_EXIST_CODE, ErrorCode.ROLE_EXIST_MESS);

            }

            logService.save(new Log(Log.INSERT_ACTION, "新增供应商:" + customer.getCustomerName()));

            customerDao.insertCustomer(customer);

        } else {

            logService.save(new Log(Log.UPDATE_ACTION, "修改顾客:" + customer.getCustomerName()));

            customerDao.updateCustomer(customer);

        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);

    }


//    @Override
//    public Map<String, Object> listAll() {
//        Map<String,Object> map = new HashMap<>();
//
//        List<Role> roleList = roleDao.findAll();
//
//        logService.save(new Log(Log.SELECT_ACTION, "查询所有角色信息"));
//
//        map.put("rows", roleList);
//
//        return map;
//    }
//
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {
        Map<String, Object> map = new HashMap<>();
        int total = customerDao.getCustomerCount(customerName);
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Customer> customer = customerDao.getCustomerList(offSet, rows, customerName);
        logService.save(new Log(Log.SELECT_ACTION, "分页查询角色信息"));
        map.put("total", total);
        map.put("rows", customer);
        return map;
    }



    @Override
    public ServiceVO delete(String ids) {
        String[] arr = ids.split(",");
        for (int i = 0; i < arr.length;i++) {
            int id=Integer.parseInt(arr[i]);
            logService.save(new Log(Log.DELETE_ACTION, "删除客户:" +
                    customerDao.getCustomerById(id).getCustomerName()));
            customerDao.deleteCustomer(id);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

//    @Override
//    public ServiceVO setMenu(Integer roleId, String menus) {
//        // 先删除当前角色的所有菜单
//        roleMenuDao.deleteRoleMenuByRoleId(roleId);
//
//        // 再赋予当前角色新的菜单
//        String[] menuArray = menus.split(",");
//
//        for(String str : menuArray){
//
//            RoleMenu rm = new RoleMenu();
//
//            rm.setRoleId(roleId);
//
//            rm.setMenuId(Integer.parseInt(str));
//
//            roleMenuDao.save(rm);
//
//        }
//
//        logService.save(new Log(Log.UPDATE_ACTION,"设置"+roleDao.getRoleById(roleId).getRoleName()+"角色的菜单权限"));
//
//        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
//    }
}
